package de.fhtrier.gdw2.ships;

import de.fhtrier.gdw.commons.netcode.datagram.INetDatagram;
import de.fhtrier.gdw2.ships.game.Constants;
import de.fhtrier.gdw2.ships.game.Player;
import de.fhtrier.gdw2.ships.network.NetGame;
import de.fhtrier.gdw2.ships.network.NetGameClient;
import de.fhtrier.gdw2.ships.network.NetGameServer;
import de.fhtrier.gdw2.ships.states.LoadGameState;
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.Color;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.SlickException;
import org.newdawn.slick.state.StateBasedGame;
import org.newdawn.slick.state.transition.FadeInTransition;
import org.newdawn.slick.state.transition.FadeOutTransition;
import org.newdawn.slick.state.transition.Transition;

public class SpaceshipGame extends StateBasedGame {

	private LoadGameState loadGameState;

    public static final int MAINMENUSTATE = 0;
	public static final int GAMESTATE = 1;
	public static int currentState = -1;
	
	//private Unit[] blueprints;
	
	private static NetGame netGame;
	public static boolean isServer() { return netGame != null && netGame.isServer(); }
	public static boolean isClient() { return netGame != null && netGame.isClient(); }
	
	public static SpaceshipGame instance;
	public static Player[] players;
	
	public SpaceshipGame(String[] args) {
		super("Spaceships");
	}

	@Override
	protected void preUpdateState(GameContainer container, int delta) throws SlickException {
		if(netGame != null)
			netGame.handleIncoming();
	}

	@Override
	protected void postUpdateState(GameContainer container, int delta) throws SlickException {
		if(netGame != null)
			netGame.handleOutgoing();
	}

	@Override
	public void initStatesList(GameContainer container) throws SlickException {
		loadGameState = new LoadGameState();
		addState(loadGameState);
	}

	public static void main(String[] args) {
		
		try {
			instance = new SpaceshipGame(args);
			AppGameContainer app = new AppGameContainer(instance);
			//app.setDisplayMode(Constants.COMPONENTSIZE * Constants.WINDOW_PIXI_WIDTH, Constants.COMPONENTSIZE * Constants.WINDOW_PIXI_HEIGHT, false);
			app.setDisplayMode(app.getScreenWidth() - 50, app.getScreenHeight() - 150, false);
			
			app.setAlwaysRender(true);
			app.setMaximumLogicUpdateInterval(60);
			app.setVSync(true);
			app.setShowFPS(true);
            
			app.start();
		} catch (SlickException e) {
			e.printStackTrace();
		}

	}

	public void createServer(String ip, int port) {
		netGame = new NetGameServer(ip, port);
	}

	public void createClient(String ip, int port, String playerName) {
		netGame = new NetGameClient(ip, port, playerName);
	}

	public void disconnect() {
		if(netGame != null) {
			netGame.shutdown();
			netGame = null;
		}
        changeState(SpaceshipGame.MAINMENUSTATE);
	}
	
	public static void changeState(int id) {
		if(id != currentState) {
			Transition transitionOut = new FadeOutTransition(Color.black, 1000);
			Transition transitionIn = new FadeInTransition(Color.black, 1000);
			instance.enterState(id, transitionOut, transitionIn);
			
			currentState = id;
		}
	}
	
	public static void send(INetDatagram datagram) {
		if(netGame != null)
			netGame.send(datagram);
	}

	public static void sendEvent(byte type, short id, short param1, short param2) {
		if(netGame != null)
			netGame.sendEvent(type, id, param1, param2);
	}
}